import { find } from 'lodash-es'

function normalizeFormInputs(model, passby = []) {
  const {
    columns = [],
    formInputs: rawFormInputs = {}
  } = model || {}
  const formInputs = {}
  const formRules = {}
  const initValues = {}

  Object.keys(rawFormInputs).forEach(key => {
    if (
      Array.isArray(passby) &&
      passby.includes(key)
    ) return

    const {
      title,
      ...columnProps
    } = find(columns, ['key', key]) || {}

    const props = rawFormInputs[key]
    const $title = props.title || title || key

    if ($title) {
      formInputs[key] = {
        title: $title,
        key,
        ...columnProps,
        ...props
      }
      initValues[key] = props.initValue || null
    }

    if (props.rules) {
      formRules[key] = props.rules
    }
  })

  return {
    formInputs,
    formRules,
    initValues
  }
}

export default normalizeFormInputs
